import {inject, observer} from "mobx-react";
import React from "react";
import loadjs from "loadjs";
import {workflowPublicFiles} from "../constants/fileConfig";
import Loading from "./Loading";
import * as authTypes from "../constants/secondAuthType";
import {wfListDoingStore} from "../stores/index";

export default class Main extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            hasLoad: false,
            store: null
        };
    }

    componentDidMount() {
        const init = () => {
            const {SecondAuthStore} = window.weaWorkflowPublic;     //公共组件的store，与本组件的secondAuthStore不同，请注意
            this.setState({hasLoad: true, store: SecondAuthStore});
        }
        loadjs.isDefined('workflowPublic') ? loadjs.ready('workflowPublic', init) : loadjs(workflowPublicFiles, 'workflowPublic', init);
    }

    //验证成功后的回调方法
    onAuthSuccess(result) {
        const {wfListDoingStore, secondAuthStore} = this.props;
        wfListDoingStore.setShowSecondAuth(false);      //关闭二次验证的窗口

        const {isProtectDialog = 0, secondAuthType} = result;

        if (isProtectDialog == 1) {     //数据保护：CA或者契约锁数据保护
            secondAuthStore.setDialogResult({isProtectSuccess: 1});  //数据保护成功
        } else if (isProtectDialog == 10) {     //CA认证 + CA数据保护 或者 契约锁认证 + 契约锁数据保护
            secondAuthStore.setDialogResult({isProtectSuccess: 1});  //数据保护成功
            secondAuthStore.setDialogResult({isAuthSuccess: 1});  //二次验证成功
        } else {        //二次验证：CA或者契约锁验证
            secondAuthStore.setDialogResult({isAuthSuccess: 1});  //二次验证成功
        }

        //契约锁验证，保存印章信息
        if (secondAuthType == authTypes.QYS) {
            const {sealId: qysSealId, sealImg: qysSealImg, certDetails: qysCertDetails} = result;    //契约锁印章，印章id信息,certDetails：印章信息，json字符串
            const {params} = secondAuthStore;
            secondAuthStore.setParams({
                ...params,
                qysSealId,
                qysSealImg,
                qysCertDetails: JSON.stringify(qysCertDetails)
            });
        }

        const {callbackFunc, callbackParam} = secondAuthStore;
        if (callbackFunc && typeof callbackFunc == "function") {
            callbackFunc.call({}, callbackParam);
        }
    }

    //验证失败后的回调方法
    onAuthFail() {
        //nothing to do
    }

    //关闭窗口
    onClose() {
        const {wfListDoingStore, secondAuthStore} = this.props;
        wfListDoingStore.setShowSecondAuth(false);      //关闭二次验证的窗口
        secondAuthStore.resetStore();
    }

    render() {
        const {hasLoad, store} = this.state;

        const {wfListDoingStore, secondAuthStore} = this.props;
        const {params, protectType, isProtectDialog} = secondAuthStore;
        let {secondAuthType} = secondAuthStore;

        const {authKey = "", signUrl = ""} = params;


        const {visible} = this.props;
        if (hasLoad) {
            const {SecondAuthCom} = window.weaWorkflowPublic;

            if (isProtectDialog == 1) {
                secondAuthType = protectType;
            }

            store.setAuthData({authKey, secondAuthType, isProtectDialog, signUrl});
            return (
                <SecondAuthCom ecId={`${this && this.props && this.props.ecId || ''}_SecondAuthCom@e5p22m`}
                    visible={visible}
                    secondAuthStore={store}
                    onSuccess={this.onAuthSuccess.bind(this)}
                    onFail={this.onAuthFail.bind(this)}
                    onClose={this.onClose.bind(this)}/>
            )
        } else {
            return (<Loading ecId={`${this && this.props && this.props.ecId || ''}_Loading@5w67ja`}/>);
        }
    }
}